package com.youmu.sichuansfc.action;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;


@Controller
public class TmpAction {
	
	
	private Logger logger = Logger.getLogger(this.getClass());
    
    @RequestMapping({ "/m/tmp_cancel_risk.htm" })
	public void ajax_goods_info(HttpServletRequest request, HttpServletResponse response, 
			Long companyId,
			String codes,
			String remark) {
    	String msg = "fail";
    	if(companyId != null && codes != null && !"".equals(codes)){
    		try {
				JdbcUtil.getInstance().getConnection();
				String sql = "select * from youmu_company where id=?";
				Map<String,Object> c = JdbcUtil.getInstance().findSimpleResult(sql, companyId);
				if(c != null){
					Double compliance_score = (Double) c.get("compliance_score");
					if(compliance_score == null)
						compliance_score = 0.0;
					Long id = (Long) c.get("id");
					String[] codeArr = codes.split(",");
					for(String code : codeArr){
						sql = "update youmu_company_risk set is_delete=1 where company_id=? and problem_code=?";
						int len = JdbcUtil.getInstance().updateByPreparedStatement(sql, true, id, code);
						if(len > 0){
							sql = "insert into youmu_risk_op(add_time,problem_code,company_id,remark) values(now(),?,?,?)";
							JdbcUtil.getInstance().updateByPreparedStatement(sql, true, code, id, remark);
						}
					}
					
					//计算分数
					/*
						1.只要存在风险为失联机构，名单类型为黑名单，合规扣90不变；
						2.多条风险解除部分风险剩余大于等于3条风险 分数不增加，名单类型不变
						3.解除后剩余2条风险，合规分数增加40分，名单类型为黑名单
						4.解除后剩余1条风险，合规分数增加70分，名单类型变为灰名单
						5.解除后剩余0条风险，合规分数变为90分，名单类型变为正常。
					 * */
					
					Long lost = (Long) JdbcUtil.getInstance().findSimpleResult(
							"select count(*) c from youmu_company_risk where detail like '%失联%' and is_delete=0 and company_id=?",id).get("c");
					if(lost == 0){
						Long riskLen = (Long) JdbcUtil.getInstance().findSimpleResult(
								"select count(*) c from youmu_company_risk where company_id=? and is_delete=0", id).get("c");
						if(riskLen==2){
							JdbcUtil.getInstance().updateByPreparedStatement(
									"update youmu_company set compliance_score=?,company_status=4 where id=?", true, 40, id);
						}else if(riskLen==1){
							JdbcUtil.getInstance().updateByPreparedStatement(
									"update youmu_company set compliance_score=?,company_status=3 where id=?", true, 70, id);
						}else if(riskLen==0){
							JdbcUtil.getInstance().updateByPreparedStatement(
									"update youmu_company set compliance_score=?,company_status=1 where id=?", true, 90, id);
						}
					}
					
					msg = "succ";
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally{
				JdbcUtil.getInstance().releaseConn();
			}
        	
    	}
		
		response.setContentType("text/plain");
		response.setHeader("Cache-Control", "no-cache");
		response.setCharacterEncoding("UTF-8");
		try {
			PrintWriter writer = response.getWriter();
			writer.print(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
    
}
